<%
    def columns=tableDefine.columns
    def varDomainName=tableNameUtil.lowerCaseFirst(tableDefine.id)
    def urlPrefix=PubUtils.addStrBeforeSeparator(PubUtils.packageToPath(config.category),".")+varDomainName
%>
<template>
    <div>
        <el-dialog title="修改 ${tableDefine.cnname}" :visible.sync="dialogVisible">
            <el-form ref="editForm" :model="form" :rules="rules">
                <% columns.each{
                    def column=it;

                    if(tableModel.isNotInList('updateList',column.columnName)){
                        return ;
                    }
                %>
                <el-form-item label="${column.cnname}" :label-width="formLabelWidth" prop="${column.dataName}">
                    <el-input v-model="form.${column.dataName}" auto-complete="off" :maxlength="${column.length}" :disabled="${column.getIsPK()}"/>
                </el-form-item>
                <%

                    }
                %>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button :loading="loading" @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    var _ = require('underscore')

    var validateSet = {
<%
			def listSize=tableModel.listSize('updateList');
		 	int i=0;
			columns.each{
			  def column=it;
			  if(tableModel.isNotInList('updateList',column.columnName)){
				return ;
			  }
			  if(column.getIsPK()){
			  	i++;
				return ;
			  }

		    print snippetTemplateUtil.getTemplate(column,'form_rule_item_vue');
		 	i++;
			if(i<listSize) println ',';

		}
		%>
    };

    let formItems = {
<%
		 	i=0;
			columns.each{
			  def column=it;
			  if(tableModel.isNotInList('addList',column.columnName)){
				return ;
			  }
		    print """\t\t${column.dataName}: null"""
		 	i++;
			if(i<listSize) println ',';

		}
		%>
    };

    export default {
        data() {
            return {
                loading: false,
                dialogVisible: false,
                form: _.clone(formItems),
                rules: validateSet,
                formLabelWidth: '120px'
            }
        },
        methods: {
            show(isShow) {
                this.dialogVisible = isShow
            },
            submitForm() {
                const _this = this
                this.\$refs['editForm'].validate((valid) => {
                    if (valid) {
                        _this.loading = true
                        const promise = new Promise(function(resolve, reject) {
                            // remote ajax request ,need implement
                            setTimeout(function() {
                                _this.loading = false
                                resolve({ 'success': true })
                            }, 1000)
                        })
                        promise.then(function(data) {
                            // this.\$Message.success('新增成功！')
                            alert('修改成功')
                            _this.show(false)
                            _this.\$emit('refreshList')
                        }).catch(function() {
                            this.\$Message.error('修改失败！')
                        })
                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            },
            editItem(item){
                this.form = item;
                this.show(true);
            }
        }
    }
</script>
